﻿@model GoldUSD.Models.PriceGridModel
@{
    foreach (var priceType in Model.PriceTypes)
    {
        var priceGrid = new WebGrid(canSort: false, canPage: false);
        if (priceType.Prices.Any())
        {
            priceGrid.Bind(priceType.Prices, autoSortAndPage: false);
        }
        else
        {
            priceGrid = null;
        }

        if (priceGrid != null)
        {
            var symbolAttr = new Dictionary<string, object>();
            symbolAttr.Add("data-val-required", "Mã không được để trống");
            symbolAttr.Add("data-val", "true");
            var buyingAttr = new Dictionary<string, object>();
            buyingAttr.Add("data-val-required", "Giá mua vào không được để trống");
            buyingAttr.Add("data-val", "true");
            buyingAttr.Add("data-val-regex", "Giá mua vào phải là số nguyên dương");
            //buyingAttr.Add("data-val-regex-pattern", @"(0|[1-9]{1}[0-9]{0,8}|[1]{1}[0-9]{1,9}|[-]{1}[2]{1}([0]{1}[0-9]{8}|[1]{1}([0-3]{1}[0-9]{7}|[4]{1}([0-6]{1}[0-9]{6}|[7]{1}([0-3]{1}[0-9]{5}|[4]{1}([0-7]{1}[0-9]{4}|[8]{1}([0-2]{1}[0-9]{3}|[3]{1}([0-5]{1}[0-9]{2}|[6]{1}([0-3]{1}[0-9]{1}|[4]{1}[0-8]{1}))))))))|(\+)?[2]{1}([0]{1}[0-9]{8}|[1]{1}([0-3]{1}[0-9]{7}|[4]{1}([0-6]{1}[0-9]{6}|[7]{1}([0-3]{1}[0-9]{5}|[4]{1}([0-7]{1}[0-9]{4}|[8]{1}([0-2]{1}[0-9]{3}|[3]{1}([0-5]{1}[0-9]{2}|[6]{1}([0-3]{1}[0-9]{1}|[4]{1}[0-7]{1})))))))))");
            buyingAttr.Add("data-val-range-min", "0");
            buyingAttr.Add("data-val-range-max", "2147483647");
            buyingAttr.Add("data-val-range", "Giá mua vào phải là số nguyên dương");

            var sellingAttr = new Dictionary<string, object>();
            sellingAttr.Add("data-val-required", "Giá bán ra không được để trống");
            sellingAttr.Add("data-val", "true");
            sellingAttr.Add("data-val-regex", "Giá bán ra phải là số nguyên dương");
            //sellingAttr.Add("data-val-regex-pattern", @"(0|[1-9]{1}[0-9]{0,8}|[1]{1}[0-9]{1,9}|[-]{1}[2]{1}([0]{1}[0-9]{8}|[1]{1}([0-3]{1}[0-9]{7}|[4]{1}([0-6]{1}[0-9]{6}|[7]{1}([0-3]{1}[0-9]{5}|[4]{1}([0-7]{1}[0-9]{4}|[8]{1}([0-2]{1}[0-9]{3}|[3]{1}([0-5]{1}[0-9]{2}|[6]{1}([0-3]{1}[0-9]{1}|[4]{1}[0-8]{1}))))))))|(\+)?[2]{1}([0]{1}[0-9]{8}|[1]{1}([0-3]{1}[0-9]{7}|[4]{1}([0-6]{1}[0-9]{6}|[7]{1}([0-3]{1}[0-9]{5}|[4]{1}([0-7]{1}[0-9]{4}|[8]{1}([0-2]{1}[0-9]{3}|[3]{1}([0-5]{1}[0-9]{2}|[6]{1}([0-3]{1}[0-9]{1}|[4]{1}[0-7]{1})))))))))");
            sellingAttr.Add("data-val-range-min", "0");
            sellingAttr.Add("data-val-range-max", "2147483647");
            sellingAttr.Add("data-val-range", "Giá bán ra phải là số nguyên dương");
            <strong>@priceType.Name</strong>
            @priceGrid.GetHtml(htmlAttributes: new {border = "1px;"}, columns: new[]
                                                                                   {
                                                                                       priceGrid.Column(header:"Mã", format: @<text>@Html.TextBox("symbol-" + (int)item.Id, (string)item.Symbol, symbolAttr)@Html.Hidden("hiddenPrice-" + (int)item.Id, (int)item.Id)</text>),
                                                                                       priceGrid.Column(header:"Mua vào", format: @<text>@Html.TextBox("buyingPrice-" + (int)item.Id, (double)item.BuyingPrice, buyingAttr)</text>),
                                                                                       priceGrid.Column(header:"Bán ra", format: @<text>@Html.TextBox("sellingPrice-" + (int)item.Id, (double)item.SellingPrice, sellingAttr)</text>),
                                                                                       priceGrid.Column(header: "", format: item => MvcHtmlString.Create(string.Format("<a href='javascript:void(0)' onclick='deletePrice({0})' >Xóa</a> <a class='action' href='{1}'>Sửa</a>", item.Id, Url.Action("EditPrice", "Admin", new {id = item.Id})))),

                                                                                   })
            <br/>
        }
    }
}
<script type="text/javascript">
    $(function () {
        //Re-apply unobtrusive jquery validation, it doesn't work when loading partial view by ajax.
        $("form").removeData("validator");
        $("form").removeData("unobtrusiveValidation");
        $.validator.unobtrusive.parse("form");
        @if (TempData["SaveSuccess"] != null)
        {
            @:alert("Tỉ giá lưu thành công!");
            @:location.reload(); 
        }
        $('.action').click(function () {
            $("#container").load($(this).attr('href'));
            return false;
        });
    });
</script>